package com.ad.pet.app.mall;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.data.injector.PerActivity;
import com.ad.pet.common.structure.mvp.AbsBasePresenter;
import com.ad.pet.models.remote.CommList;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.remote.cart.Good;
import com.ad.pet.models.remote.mall.ProductDetail;
import com.ad.pet.models.remote.mall.Rating;
import com.ad.pet.models.remote.mall.Record;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by SEELE on 2018/3/27.
 */
@PerActivity
public class MallPresenter extends AbsBasePresenter implements MallContracts.Presenter {

    private ApiService mApiService;
    private MallContracts.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;

    @Inject
    public MallPresenter(ApiService apiService,
                         Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }

    @Override
    public void onCreate(MallContracts.View view) {
        this.mView = view;
    }


    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
            mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }

    @Override
    public void getGoodDetail(String product_id) {
        mRxDisposable.add(mApiService.getProductDetial(product_id).subscribe(new AppSubscriber<ProductDetail>(mView) {
            @Override
            public void next(ProductDetail productDetail) {
                mView.getGoodDetailSuceess(productDetail);
            }
        }));
    }

    @Override
    public void getRateList(String product_id, String page, String size) {
        mRxDisposable.add(mApiService.getRatingList(product_id, page, size).subscribe(new AppSubscriber<CommList<List<Rating>>>(mView) {
            @Override
            public void next(CommList<List<Rating>> productDetail) {
                mView.getRateListSuccess(productDetail.getList());
            }
        }));
    }

    @Override
    public void getSaleRecord(String product_id, String page, String size) {
        mRxDisposable.add(mApiService.getSalesRecords(product_id, page, size).subscribe(new AppSubscriber<CommList<List<Record>>>(mView) {
            @Override
            public void next(CommList<List<Record>> productDetail) {
                mView.getSaleRecordSuccess(productDetail.getList());
            }
        }));
    }

    @Override
    public void addToCart(String pid, String number, String sku,String storeId) {
        mRxDisposable.add(mApiService.addToCart(pid, number, sku,storeId).subscribe(new AppSubscriber<User>(mView) {
            @Override
            public void next(User productDetail) {
                mView.addToCartSuccess(productDetail);
            }
        }));
    }

    @Override
    public void getFlashList() {
        mRxDisposable.add(mApiService.getProductList().subscribe(new AppSubscriber<CommList<List<Good>>>(mView) {
            @Override
            public void next(CommList<List<Good>> productDetail) {
                mView.getFlashListSuccess(productDetail);
            }
        }));
    }


}
